package com.example.home.rubbish.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.home.R;
import com.example.home.hospital.adapter.NewsRcvListAdapter;
import com.example.home.rubbish.bean.RcvListBean;
import com.example.home.rubbish.bean.SearchNameBean;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import static com.example.home.tool.StaticActi.ip;

public class SearchDetailActivity extends AppCompatActivity {

    private ImageView back;
    private TextView name;
    private ImageView imgUrl;
    private TextView introduce;
    private RecyclerView rcvList;
    private TextView guide;
    private SearchNameBean.RowsBean item;
    private Integer id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_detail);
        // 获取通过Intent传递的数据
        Intent intent = getIntent();
        if (intent != null) {
            // 获取name参数
            String nameJson = intent.getStringExtra("name");

            // 使用Gson将JSON字符串转换为对象
            Gson gson = new Gson();
            item = gson.fromJson(nameJson, SearchNameBean.RowsBean.class);

            // 现在你可以使用item对象中的数据了
            if (item != null) {
                String itemName = item.getName();
                // 其他数据的获取方式类似
            }
        }
        initView();
        id = item.getId();
        getRcvList();
    }

    private void getRcvList() {
        OKHttpTool.doNetRequest("prod-api/api/garbage-classification/garbage-example/list?type="+id,
                OKHttpTool.GET,
                RcvListBean.class,
                new OKHttpTool.NetCallback() {
                    @Override
                    public void onSuccess(Object obj) {
                        RcvListBean rcvListBean = (RcvListBean) obj;
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                bindList(rcvListBean);
                            }
                        });
                    }

                    @Override
                    public void onFailure(String msg) {

                    }
                });
    }

    private void bindList(RcvListBean rcvListBean) {
        NewsRcvListAdapter newsRcvListAdapter = new NewsRcvListAdapter(SearchDetailActivity.this, rcvListBean.getRows());
        GridLayoutManager gridLayoutManager = new GridLayoutManager(SearchDetailActivity.this, 4);
        rcvList.setLayoutManager(gridLayoutManager);
        rcvList.setAdapter(newsRcvListAdapter);
    }

    private void initView() {

        back = findViewById(R.id.back);
        name = findViewById(R.id.name);
        imgUrl = findViewById(R.id.imgUrl);
        introduce = findViewById(R.id.introduce);
        rcvList = findViewById(R.id.rcv_list);
        guide = findViewById(R.id.guide);

        name.setText(item.getName());
        guide.setText(item.getGuide());
        Glide.with(SearchDetailActivity.this).load(ip + item.getImgUrl()).into(imgUrl);
        introduce.setText(item.getIntroduce());

        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SearchDetailActivity.this.finish();
            }
        });

    }
}